// pages/index/parkingplacedetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    addFLag:false,
    radioItems: [
      // { name: 'cell standard', value: '0', checked: true },
      // { name: 'cell standard', value: '1' }
    ],
    checkboxItems: [{
        name: 'standard is dealt for u.',
        value: '0',
        checked: true
      },
      {
        name: 'standard is dealicient for u.',
        value: '1'
      }
    ],
    items: [{
        name: 'USA',
        value: '美国'
      },
      {
        name: 'CHN',
        value: '中国',
        checked: 'true'
      },
      {
        name: 'BRA',
        value: '巴西'
      },
      {
        name: 'JPN',
        value: '日本'
      },
      {
        name: 'ENG',
        value: '英国'
      },
      {
        name: 'TUR',
        value: '法国'
      },
    ],

    date: "2016-09-01",
    time: "12:01",

    countryCodes: ["+86", "+80", "+84", "+87"],
    countryCodeIndex: 0,

    countries: ["中国", "美国", "英国"],
    countryIndex: 0,

    accounts: ["微信号", "QQ", "Email"],
    accountIndex: 0,

    isAgree: false,
    formData: {
      status: true
    },
    rules: [{
      name: 'radio',
      rules: {
        required: true,
        message: '小区是必选项'
      },
    }, {
      name: 'price',
      rules: {
        required: true,
        message: '请输入价格'
      },
    }, {
      name: 'location',
      rules: {
        required: true,
        message: '请输入位置'
      },
    }, {
      name: 'timeStart',
      rules: {
        required: true,
        message: '请输入开始时间'
      },
    }, {
      name: 'timeEnd',
      rules: {
        required: true,
        message: '请输入结束时间'
      },
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

    wx.request({
      url: getApp().data.servser + '/parking/list',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: function(res) {
        var radioItems = new Array();
        for (var i = 0; i < res.data.data.parkingInfos.length; i++) {
          var obj = new Object();
          obj.name = res.data.data.parkingInfos[i].name;
          obj.value = res.data.data.parkingInfos[i].seqId;
          radioItems.push(obj);
        }
        that.setData({
          radioItems: radioItems
        })
      }
    })
    console.log(options.id);
    if (options.id == -1) {
      let parkingId = options.parkingId;
      that.setData({
        location: '',
        checked_status: true,
        seqId: '',
        addFLag : true,
        parkingId: parkingId
      })
    } else {
      wx.request({
        url: getApp().data.servser + '/parking/place/detail',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          openId: app.globalData.openid,
          seqId: options.id
        },
        success: function(res) {
          that.setData({
            parkingPlaceUseInfo: res.data.data.parkingPlaceUseInfo,
            location: res.data.data.parkingPlaceUseInfo.location,
            [`formData.location`]: res.data.data.parkingPlaceUseInfo.location,
            price: res.data.data.parkingPlaceUseInfo.price,
            [`formData.price`]: res.data.data.parkingPlaceUseInfo.price,
            timeStartStr: res.data.data.parkingPlaceUseInfo.timeStartStr,
            [`formData.timeStart`]: res.data.data.parkingPlaceUseInfo.timeStartStr,
            timeEndStr: res.data.data.parkingPlaceUseInfo.timeEndStr,
            [`formData.timeEnd`]: res.data.data.parkingPlaceUseInfo.timeEndStr,
            checked_status: res.data.data.parkingPlaceUseInfo.status == '3' ? false : true,
            [`formData.status`]: res.data.data.parkingPlaceUseInfo.status == '3' ? false : true,
            seqId: res.data.data.parkingPlaceUseInfo.seqId,
            parkingId: res.data.data.parkingPlaceUseInfo.parkingId,
            memo: res.data.data.parkingPlaceUseInfo.memo,
            [`formData.radio`]: res.data.data.parkingPlaceUseInfo.parkingId
          })
        }
      })
    }

  },
  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  bindDateChange: function(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
      //[`formData.date`]: e.detail.value
    })
  },
  radioChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems,
      [`formData.radio`]: e.detail.value
    });
  },
  submitForm() {
    var that = this;
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          this.setData({
            error: errors[firstError[0]].message
          })

        }
      } else {
        console.log(11111111111111);
        if (that.data.addFLag){
          wx.request({
            method: "POST",
            url: getApp().data.servser + '/parking/place/addItem',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              memo: that.data.formData.memo,
              location: that.data.formData.location,
              price: that.data.formData.price,
              timeStart: "2020-02-02 " + that.data.formData.timeStart,
              timeEnd: "2020-02-02 " + that.data.formData.timeEnd,
              status: that.data.formData.status ? '0' : '3',
              openId: app.globalData.openid,
              parkingId: that.data.formData.radio
            },
            success: function (res) {
              wx.showToast({
                title: "新增成功",
                icon: 'none',
                duration: 2000
              })
              wx.reLaunch({
                url: 'parkingplacelist'
              })
            }
          })
        }else{
          console.log('update');
          wx.request({
            method: "POST",
            url: getApp().data.servser + '/parking/place/editItem',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              seqId: that.data.seqId,
              memo: that.data.formData.memo,
              location: that.data.formData.location,
              price: that.data.formData.price,
              timeStart: "2020-02-02 " + that.data.formData.timeStart,
              timeEnd: "2020-02-02 " + that.data.formData.timeEnd,
              status: that.data.formData.status ? '0' : '3',
              openId: app.globalData.openid,
              parkingId: that.data.formData.radio
            },
            success: function (res) {
              wx.showToast({
                title: "更新成功",
                icon: 'none',
                duration: 2000
              })
              wx.reLaunch({
                url: 'parkingplacelist'
              })
            }
          })
        }

      }
    })
    // this.selectComponent('#form').validateField('mobile', (valid, errors) => {
    //     console.log('valid', valid, errors)
    // })
  },
  delete: function() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            method: "POST",
            url: getApp().data.servser + '/parking/place/delete',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              seqId: that.data.seqId
            },
            success: function(res) {
              wx.showToast({
                title: "删除成功",
                icon: 'none',
                duration: 2000
              })
              wx.reLaunch({
                url: 'register'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  update: function() {
    let that = this;
    if (that.data.seqId == '') {

    } else {
      wx.request({
        method: "POST",
        url: getApp().data.servser + '/parking/place/editItem',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          seqId: that.data.seqId,
          location: that.data.location,
          status: that.data.checked_status ? '0' : '3',
          parkingId: that.data.parkingId
        },
        success: function(res) {
          wx.showToast({
            title: "新增成功",
            icon: 'none',
            duration: 2000
          })
          wx.reLaunch({
            url: 'register'
          })
        }
      })
    }
  },
  add: function() {
    let that = this;
    wx.request({
      method: "POST",
      url: getApp().data.servser + '/parking/place/addItem',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        location: that.data.location,
        status: that.data.checked_status ? '0' : '3',
        parkingId: that.data.parkingId
      },
      success: function(res) {
        wx.showToast({
          title: "新增成功",
          icon: 'none',
          duration: 2000
        })
        wx.reLaunch({
          url: 'register'
        })
      }
    })
  },
  touch_status: function(e) {
    this.data.checked_status = e.detail.value;
  },
  getInputLocation: function(e) {
    this.data.location = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})